package com.example.client;

import javafx.fxml.FXML;

import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;


public class ClientLogController {

    @FXML
    private TableView<ClientData> clientDataTable;
    @FXML
    private TableColumn<ClientData, String> dateColumn;
    @FXML
    private TableColumn<ClientData,String> timeColumn;

    @FXML
    private Label temperatureLabel;
    @FXML
    private Label humidityLabel;
    @FXML
    private Label co2Label;
    @FXML
    private Label luxLabel;

    ClientLog clientLog;

    //默认构造函数
    public ClientLogController(){
    }


    public void setClientLog(ClientLog clientLog){
        this.clientLog = clientLog;
        clientDataTable.setItems(clientLog.getData());
    }


    private void showDataDetails(ClientData clientData){
        if(clientData!=null){
            System.out.println("该条数据不为空，采集日期为：" + clientData.getDate());
            //补充表格右边的详细信息
            temperatureLabel.setText(clientData.getTemperature());
            humidityLabel.setText(clientData.getHumidity());
            co2Label.setText(clientData.getCo2());
            luxLabel.setText(clientData.getLux());
        }else{
            System.out.println("该条数据为空");
            temperatureLabel.setText("0");
            humidityLabel.setText("0");
            co2Label.setText("0");
            luxLabel.setText("0");
        }
    }

    @FXML
    public void initialize(){
        System.out.println("初始化表格");
        ////我们在表格列上使用setCellValueFactory(...) 来确定为特定列使用ClientData对象的某个属性
        dateColumn.setCellValueFactory(cellData -> cellData.getValue().dateProperty());
        timeColumn.setCellValueFactory(cellData -> cellData.getValue().timeProperty());

        //数据初始化为空字符串
        showDataDetails(null);
        //为table绑定事件监听，点击条目后修改当前的显示数据
        clientDataTable.getSelectionModel().selectedItemProperty().addListener(
                (observable, oldValue, newValue) -> showDataDetails(newValue)
        );

    }

}
